package com.zenika.zentracker.hibernate.info;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * This class is used to collect information in the case Hibernate Recreates a Collection.
 * 
 * @see CollectionInformation
 * @see AbstractCollectionInformation
 * @author Zenika
 */
public class CollectionRecreate extends AbstractCollectionInformation implements CollectionInformation {

    private List addedObjects = new ArrayList();

    /**
     * The flush index and the new collection are needed to create a CollectionRecreate Information.
     * 
     * @param flushNumber the flush index before a transaction completion
     * @param newlyObjects the newly added objects into the collection
     */
    public CollectionRecreate(final int flushNumber, final Serializable newObjects) {
        super(flushNumber);
        if (newObjects instanceof Map) {
            this.addedObjects.addAll(((Map) newObjects).values());
        } else {
            this.addedObjects.addAll(((List) newObjects));
        }
    }

    /**
     * The flush index and the new collection are needed to create a CollectionRecreate Information.
     * 
     * @param flushNumber the flush index before a transaction completion
     * @param newlyObjects the newly added objects into the collection
     */
    public CollectionRecreate(final int flushNumber, final Map newObjects) {
        super(flushNumber);
        this.addedObjects.addAll(newObjects.values());
    }

    public String toString() {
        return logAddingElementsInCollection(addedObjects);
    }

}
